#include "PreComplie.h"
#include "MsgLoop.h"
#include "Timer.h"

MSG CMsgLoop::m_msg;

CMsgLoop::CMsgLoop():m_start(false), m_stop(false)
{
}

CMsgLoop::~CMsgLoop()
{
}

int CMsgLoop::Enter()
{
	if(m_start) return -1;
	m_start = true;
	while(!m_stop)
	{
		__int64 start = time_ms();
		while(PeekMessageW(&m_msg, NULL, 0, 0, PM_REMOVE))
		{
			if(m_msg.message == WM_QUIT)
				m_stop = true;
			TranslateMessage(&m_msg);
			DispatchMessageW(&m_msg);
		}
		Sleep(5);
		__int64 end = time_ms();
		DWORD consumeTime = end - start;
		if(consumeTime < 1000 / 60)
			Sleep(1000 / 60 - consumeTime);
	}
	return m_msg.message;
}

void CMsgLoop::Leave()
{
	m_stop = true;
}
